本文共 7991 字,大约阅读时间需要 26 分钟。
代码不朽
by David Yu
由于大伟
Didn’t we already fix that?
我们不是已经解决了吗?
The question that you or your teammate would ask after the product manager posts a snapshot of the bug.
产品经理发布错误快照后,您或您的队友会问的问题。
This whole event feels like a Deja Vu. You try to retrace the time you fixed that bug in the commits, but what’s the point?
整个事件感觉就像Deja Vu。 您尝试回溯在提交中修复该错误的时间,但是有什么意义呢?
The reality is if you don’t get to the root cause of the bug, it will come back like the Hydra of Lerna.
现实是,如果您没有找到该错误的根本原因,它将像Lerna的九头蛇一样回来。
Here are some patterns that lead to immortal bugs.
这是导致不朽错误的一些模式。
When we’re doing front-end development, we often use dummy data to build out the user interface quickly. That’s fine.
在进行前端开发时,我们经常使用虚拟数据来快速构建用户界面。 没关系。
The problem is having dummy data at multiple locations. Then it’s easy to forget one or two when you push your code.
问题是在多个位置都有伪数据。 然后,在推送代码时很容易忘记一两个。
Here are some tips:
这里有一些提示:
Write a comment like // TODO: remove later
to allow easy search later
编写类似// TODO: remove later
,以便以后轻松搜索
// ?
// This will break if fileName is an unexpected string
// ?
let img;
try {
img = require(`./assets/${backendData.fileName}.png`)
} catch(e) { // Catch error if file doesn't exist
}
// ?
Your codebase will only grow larger and larger as you work on it. Your client or your boss won’t know the negative effect of not refactoring the code, because everything looks fine on the surface.
您的代码库只会随着您的工作而变得越来越大。 您的客户或老板不会知道不重构代码的负面影响,因为表面上看起来一切都很好。
Plus, it’s always more satisfying to write something new and show it to other people. “Hey, check out what it can do now.”
另外,写一些新的东西并展示给其他人总是很满意的。 “嘿,看看它现在能做什么。”
So how do we know when to refactor the code?
那么我们如何知道何时重构代码?
According to , follow the Rule of Three
根据说法,遵循三法则
This goes along with code refactor. When the project is fresh, it’s difficult to predict how large a feature will become.
这与代码重构一起。 当项目刚出现时,很难预测功能的规模。
In the past, I had a file that grew into thousands of lines of code. Refactoring that code is like performing surgery, delicate, but rewarding at the end.
过去,我有一个文件,后来变成了数千行代码。 重构该代码就像执行外科手术一样,精致但最终会有所回报。
Most people have a principle of how their project is structured. Separation of files by pages, features, dev. or production, private or public method, MVC model, etc.
大多数人都有一个关于项目结构的原则。 按页面,功能,开发文件分隔文件。 或生产,私有或公共方法,MVC模型等。
How to structure a project folder is a large discussion by itself. I will save that for another article.
如何构造项目文件夹本身就是一个大讨论。 我将其保存为另一篇文章。
“Wait, why don’t the pictures show up anymore?” the product manager asks the front-end developers.
“等等,为什么这些图片不再显示?” 产品经理询问前端开发人员。
“Wait, why doesn’t the data have the picture_url anymore?” The front-end developer checks the console for network response.
“等等,为什么数据不再有picture_url了?” 前端开发人员检查控制台的网络响应。
“Oh yea, now the pictures come in three sizes. large_pic_url, med_pic_url, small_pic_url.” The back-end developer heard the discussion and jumped in.
“哦,是的,现在这些图片有三种尺寸。 large_pic_url,med_pic_url,small_pic_url。” 后端开发人员听完了讨论并跳了进来。
Sounds familiar?
听起来很熟悉?
Everyone is trying to do their job. But synergy won’t happen in the silo. It’s everyone’s job to communicate what’s needed.
每个人都在努力工作。 但是协同作用不会在筒仓中发生。 交流需要的东西是每个人的工作。
Here are some simple solutions to start.
这里是一些简单的解决方案。
When the API is completed, generate the document with
API完成后,请使用生成文档
There are more sophisticated approaches to writing specs and handling documentation. And I would love to hear about what approach your team uses in the comment section.
有更复杂的方法来编写规范和处理文档。 我很想在评论部分中了解您的团队使用的方法。
Breaking things is less of a worry since you can always revert back to the previous commit.
无需担心,因为总可以恢复到上一次提交。
It’s your or your teammate’s code disappearing during the process that’s the problem.
问题出在您或您队友的代码消失。
This often happens when you want to “save time” and move forward.
当您想“节省时间”并前进时,通常会发生这种情况。
When in doubt, get in contact with the developer who made the commit that conflicts with yours.
如有疑问,请与做出与您的冲突的提交的开发人员联系。
When you mess up, git merge --abort
or git-reset --hard
.
当您搞砸时, git merge --abort
或git-reset --hard
。
When it breaks beyond repair, remove the project and clone it again.
当它无法修复时,请删除该项目并再次克隆它。
Think twice when doing git push -f
.
做git push -f
时请三思。
Reusable components are the magical trick up every developer’s sleeve. Like when the client pitches you the wireframe that contains the same date picker across several pages.
可重用的组件是每个开发人员的魔术。 就像客户向您推销时一样,跨多个页面包含相同日期选择器的线框。
In your mind, you think, “I am going to make an awesome date picker component. Write less code. Do more.”
在您看来,您会认为:“我将制作一个很棒的日期选择器组件。 编写更少的代码。 多做。”
A few months later, the client says, “I want the date picker in this page to explode with fireworks and on other pages, different kittens to represent months”. Now you need the date picker to be more dynamic than it was.
几个月后,客户说:“我希望此页面中的日期选择器因烟花而爆炸,在其他页面上,不同的小猫代表几个月。” 现在,您需要使日期选择器更具动态性。
Then after you make the changes, you discover that fireworks are coming out of the kittens.
然后,在进行更改之后,您会发现烟花从小猫身上冒出来。
If you are on a larger team, you can defer quality assurance to a different team.
如果您的团队规模较大,则可以将质量保证推迟到其他团队。
But if there is no such functionality in your organization, you will have to do a global search for your component’s name to see what your component affects.
但是,如果您的组织中没有此类功能,则必须对组件的名称进行全局搜索,以查看组件的影响。
Make a note to yourself of those files. Test those file visually and functionally depending on what they do.
给这些文件做个记录。 根据它们的功能在视觉上和功能上测试这些文件。
You know you will have to go back to it later. The key is “don’t forget”
您知道以后将不得不重新使用它。 关键是“不要忘记”
When you decide between speed of development and stability of the software, we must be careful not to always put speed as a top priority. It’s similar to driving a car without ever checking the condition of the car.
当您决定开发速度和软件稳定性之间时,我们必须注意不要始终把速度放在第一位。 这类似于在不检查汽车状况的情况下驾驶汽车。
You can decide a ratio between speed and quality assurance. Maybe it’s two speedy iterations and one quality assurance iteration, whichever makes sense for your team.
您可以决定速度和质量保证之间的比例。 也许这是两次快速迭代和一次质量保证迭代,这对您的团队来说都是可行的。
If you want to reach the 1 billion users of WeChat, you have to understand it first. Here’s a to get started.
如果要覆盖微信的十亿用户,您必须首先了解它。 这是一个 开始。
翻译自:
代码不朽
转载地址:http://obkzd.baihongyu.com/